<!DOCTYPE html>
<html>

<head>
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta charset="utf-8"/>
    <title>EasyWeb Oauth2学习项目</title>
</head>

<body>
<div style="padding: 30px;">

    <h2>EasyWeb Oauth2学习项目</h2>
    <br><br>
    账号：<input id="username" type="text" value="admin" placeholder="账号"/><br><br>
    密码：<input id="password" type="text" value="admin" placeholder="密码"/><br><br>
    <button id="btnGetToken">获取token</button>&emsp;
    <a href="swagger-ui.html">返回swagger</a>
    <br><br>
    <div style="width: 500px;padding: 20px;overflow: auto;background: #92B8B1;border-radius: 5px;">
        <pre id="rs"></pre>
    </div>
    <br><br>
    <div>此项目为纯接口项目，需要界面可以前往<a href="https://easyweb.vip" target="_blank">easyweb.vip</a>获取</div>

</div>

<script type="text/javascript" src="jquery-3.2.1.min.js"></script>
<script type="text/javascript" src="layer/layer.js"></script>
<script>
    $(function () {
        $('#btnGetToken').click(function (obj) {
            var field = new Object();
            field.username = $('#username').val();
            field.password = $('#password').val();
            field.grant_type = 'password';
            field.scope = 'select';
            field.client_id = 'client_2';
            field.client_secret = '123456';

            layer.load(2);
            $.ajax({
                url: 'oauth/token',
                data: field,
                type: 'POST',
                dataType: 'JSON',
                success: function (data) {
                    layer.closeAll('loading');
                    console.log(JSON.stringify(data));
                    if (data.access_token) {
                        layer.msg('登录成功', {icon: 1});
                        $('#rs').html(JSON.stringify(data, null, 4));
                    } else {
                        layer.msg('登录失败，请重试', {icon: 5});
                    }
                },
                error: function (xhr) {
                    layer.closeAll('loading');
                    if (xhr.status == 400) {
                        layer.msg('账号或密码错误', {icon: 5});
                    } else {
                        layer.msg('登录失败，请按f12查看console错误信息', {icon: 5});
                    }
                }
            });
        });

    });
</script>
</body>
</html>